/* eslint-disable no-empty-function */
/* api */
import * as TaskApi from '@src/api/TaskApi.ts';
import i18n from '@src/locales';

import {
  customerAddressSelectConversion,
  customerSelectConversion,
  customerLinkmanSelectConversion,
  linkmanSelectConversion,
  productSelectConversion,
  faultSceneSelectConversion
} from '@src/util/conversionFunctionUtil.ts';


import {
  searchCustomer,
  searchAddress,
  searchLinkman,
  searchProduct,
  searchLinkmanSb
} from '@src/modules/task/edit/components/TaskEditForm/methods/initData.js'

export default {
  /**
   * @description 搜索客户地址
   */
  async searchAddress(params) {
    try {
      let result;
      if (this.justGuide)
        result = searchAddress;
      else result = await TaskApi.getTaskCustomerAddress(params);

      if (!result || !result.data) return;

      result.list = result.data.map((address) =>
        customerAddressSelectConversion(address)
      );

      return result;
    } catch (error) {
      console.warn('task-edit-form: searchAddress -> error', error);
    }
  },
  /**
   * @description 搜索客户
   */
  async searchCustomer(params = {}) {
    try {
      const payload = {
        ...params,
        queryField: ['name', 'serialNumber', 'uuid', 'customerAddress', 'lmPhone'],
      };
      const { data } = await TaskApi.getTaskCustomerListV2(payload);

      if (!data || !data.list) return;

      data.list = data.list.map(customer => customerSelectConversion(customer));

      return data;
    } catch (error) {
      console.warn('task-edit-form: searchCustomer -> error', error);
    }
  },
  /**
   * @description 搜索客户
   */
  async searchCustomerByPhone(params = {}) {
    let parameter = { phone: params.keyword };

    try {

      let result;
      if (this.justGuide) result = searchLinkmanSb
      else result = await TaskApi.getCustomerByPhone(parameter);

      if (!result || !result.data) return;

      result.list = result.data.map((linkman) =>
        customerLinkmanSelectConversion(linkman)
      );

      return result;
    } catch (error) {
      console.warn('task-edit-form: searchCustomer -> error', error);
    }
  },
  /**
   * @description 搜索联系人
   */
  async searchLinkman(params = {}) {
    try {
      let data;
      if (this.justGuide) data = searchLinkman;
      else data = await TaskApi.getTaskCustomerLinkman(params);

      let result = data.result;

      if (!result || !result.list) return;

      result.list = result.list.map((linkman) =>
        linkmanSelectConversion(linkman)
      );
      return result;
    } catch (error) {
      console.warn('task-edit-form: searchProduct -> error', error);
    }
  },
  /**
   * @description 搜索产品
   */
  async searchProduct(params = {}) {
    try {
      let result;
      if (this.justGuide)
        result = searchProduct;
      else result = await TaskApi.getTaskCustomerProduct(params);

      if (!result.result || !result.result?.list) return;

      result.result.list = result.result.list.map((product) =>
        productSelectConversion(product)
      );
      
      return result.result;
    } catch (error) {
      console.warn('task-edit-form: searchProduct -> error', error);
    }
  },
  async searchFaultScene(params = {}) {
    try {
      const faultLibrary = this.value.faultLibrary || {}
      const productTypeId = faultLibrary.productTypeId || []
      if(this.productTypeIds.length < 1 && productTypeId.length > 0) {
        this.productTypeIds = [{
          id: productTypeId[productTypeId.length - 1],
          name: faultLibrary.productType
        }]
      }
      const allParams = {
        orderDetail: {
          isSystem: 1,
          column: 'createtime',
          type: '',
          sequence: 'desc'
        },
        fasterFindId: '',
        flag: false,
        wikiTypeId: '',
        view: 'faultlibrary', // 故障库标识
        productTypeList: this.productTypeIds,
        ...params
      }
      let { result } = await TaskApi.getWikiList(allParams)
      if (!result || !result.list) return;
      result.list = result.list.map((faultScene) => 
        faultSceneSelectConversion(faultScene)
      );
      result.list.unshift({
        label: i18n.t('common.base.other'),
        faultPerformance: i18n.t('common.base.other'),
        value: -1
      })
      return result;
    } catch (error) {
      console.warn('task-edit-form: searchFaultScene -> error', error);
    }
  }
};
